//
//  NSString+JSON.m
//  YanXiuWang
//
//  Created by 慧趣小歪 on 17/1/7.
//  Copyright © 2017年 小分队. All rights reserved.
//

#import "NSString+JSON.h"
#import "NSString+Trim.h"

@implementation NSString (JSON)

- (BOOL)isJSON {
    NSString *text = self.trim;
    return
    ([text hasPrefix:@"{"] && [text hasSuffix:@"}"]) ||
    ([text hasPrefix:@"["] && [text hasSuffix:@"]"]);
}

- (BOOL)isJSONObject {
    NSString *text = self.trim;
    return [text hasPrefix:@"{"] && [text hasSuffix:@"}"];
}

- (BOOL)isJSONArray {
    NSString *text = self.trim;
    return [text hasPrefix:@"["] && [text hasSuffix:@"]"];
}

- (NSDictionary *)JSONObject {
    NSData *data = [self.trim dataUsingEncoding:NSUTF8StringEncoding];
    return [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
}

- (NSArray *)JSONArray {
    NSData *data = [self.trim dataUsingEncoding:NSUTF8StringEncoding];
    return [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
}

@end
